package com.ncwebinc.hellolistview;

import com.ncwebinc.hellolistview.base.productos;

import android.app.Activity;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

/**
 * A generic activity for editing a note in a database.  This can be used
 * either to simply view a note {@link Intent#ACTION_VIEW}, view and edit a note
 * {@link Intent#ACTION_EDIT}, or create a new note {@link Intent#ACTION_INSERT}.  
 */
@SuppressWarnings("unused")
public class Productoeditor extends Activity {
    private static final String TAG = "hello EDITOR PRODUCTO";

    /**
     * Standard projection for the interesting columns of a normal note.
     */
    private static final String[] PROJECTION = new String[] {
        productos.id_prod, // 0
        productos.cod_prod, // 1
        productos.num_serie, // 2
        productos.descrip_prod, // 3
        productos.id_subcat, // 4
        productos.nom_prod, // 5
        productos.marca_prod, // 6
        productos.mod_prod, // 7
        productos.cont_uni, // 8
        productos.uni_min, // 9
        productos.cont_uni_med, // 10
        productos.contenido, // 11
        productos.peso_neto, // 12
        productos.alto_cm, // 13
        productos.ancho_cm, // 14
        productos.fondo_cm, // 15
        productos.fabr, // 16
        productos.fam_nec, // 17
        productos.fam_prod, // 18
        productos.clase_prod, // 19
        productos.linea_prod, // 20
        productos.tipo_prod, // 21
        productos.imagen, // 22
        productos.hot, // 23
    };
    /** The index of the note column */
    private static final int COLUMN_INDEX_id_prod = 0;
    private static final int COLUMN_INDEX_cod_prod = 1;
    private static final int COLUMN_INDEX_num_serie = 2;
    private static final int COLUMN_INDEX_descrip_prod = 3;
    private static final int COLUMN_INDEX_id_subcat = 4;
    private static final int COLUMN_INDEX_nom_prod = 5;
    private static final int COLUMN_INDEX_marca_prod = 6;
    private static final int COLUMN_INDEX_mod_prod = 7;
    private static final int COLUMN_INDEX_cont_uni = 8;
    private static final int COLUMN_INDEX_uni_min = 9;
    private static final int COLUMN_INDEX_cont_uni_med = 10;
    private static final int COLUMN_INDEX_contenido = 11;
    private static final int COLUMN_INDEX_peso_neto = 12;
    private static final int COLUMN_INDEX_alto_cm = 13;
    private static final int COLUMN_INDEX_ancho_cm = 14;
    private static final int COLUMN_INDEX_fondo_cm = 15;
    private static final int COLUMN_INDEX_fabr = 16;
    private static final int COLUMN_INDEX_fam_nec = 17;
    private static final int COLUMN_INDEX_fam_prod = 18;
    private static final int COLUMN_INDEX_clase_prod = 19;
    private static final int COLUMN_INDEX_linea_prod = 20;
    private static final int COLUMN_INDEX_tipo_prod = 21;
    private static final int COLUMN_INDEX_imagen = 22;
    private static final int COLUMN_INDEX_hot = 23;
    
    // This is our state data that is stored when freezing.
    private static final String ORIGINAL_CONTENT = "origContent";

    // Identifiers for our menu items.
    private static final int ADD_ID = Menu.FIRST;
    private static final int DISCARD_ID = Menu.FIRST + 1;
    private static final int DELETE_ID = Menu.FIRST + 2;

    // The different distinct states the activity can be run in.
    private static final int STATE_EDIT = 0;
    private static final int STATE_INSERT = 1;

    private int mState;
    private boolean mNoteOnly = false;
    private Uri mUri = productos.CONTENT_URI;
    private Cursor mCursor;
    private EditText mText;
    private EditText mid_prod;
    private EditText mcod_prod;
    private EditText mnum_serie;
    private EditText mdescrip_prod;
    private EditText mid_subcat;
    private EditText mnom_prod;
    private EditText mmarca_prod;
    private EditText mmod_prod;
    private EditText mcont_uni;
    private EditText muni_min;
    private EditText mcont_uni_med;
    private EditText mcontenido;
    private EditText mpeso_neto;
    private EditText malto_cm;
    private EditText mancho_cm;
    private EditText mfondo_cm;
    private EditText mfabr;
    private EditText mfam_nec;
    private EditText mfam_prod;
    private EditText mclase_prod;
    private EditText mlinea_prod;
    private EditText mtipo_prod;
    private EditText mimagen;
    private EditText mhot; 
    private String mOriginalContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.w(TAG, "editor.............");
        super.onCreate(savedInstanceState);

        //traigo el id desde el intento
        final Intent intent = getIntent();
        int def =0;
		int id = getIntent().getIntExtra("id", def );
        

        // Set the layout for this activity.  You can find it in res/layout/note_editor.xml
        Log.e(TAG, "setContentView prodeditor  " + id);
        setContentView(R.layout.prodeditor);
        
        // asigno textbox a variable corta
        mcod_prod = (EditText) findViewById(R.id.editcod_prod);
        mnum_serie = (EditText) findViewById(R.id.editnum_serie);
        mdescrip_prod = (EditText) findViewById(R.id.editdescrip_prod);
        mid_subcat = (EditText) findViewById(R.id.editid_subcat);
        mnom_prod = (EditText) findViewById(R.id.editnom_prod);
        mmarca_prod = (EditText) findViewById(R.id.editmarca_prod);
        mmod_prod = (EditText) findViewById(R.id.editmod_prod);
        mcont_uni = (EditText) findViewById(R.id.editcont_uni);
        muni_min = (EditText) findViewById(R.id.edituni_min);
        mcont_uni_med = (EditText) findViewById(R.id.editcont_uni_med);
        mcontenido = (EditText) findViewById(R.id.editcontenido);
        mpeso_neto = (EditText) findViewById(R.id.editpeso_neto);
        malto_cm = (EditText) findViewById(R.id.editalto_cm);
        mancho_cm = (EditText) findViewById(R.id.editancho_cm);
        mfondo_cm = (EditText) findViewById(R.id.editfondo_cm);
        mfabr = (EditText) findViewById(R.id.editfabr);
        mfam_nec = (EditText) findViewById(R.id.editfam_nec);
        mfam_prod = (EditText) findViewById(R.id.editfam_prod);
        mclase_prod = (EditText) findViewById(R.id.editclase_prod);
        mlinea_prod = (EditText) findViewById(R.id.editlinea_prod);
        mtipo_prod = (EditText) findViewById(R.id.edittipo_prod);
        mimagen = (EditText) findViewById(R.id.editimagen);
        mhot = (EditText) findViewById(R.id.edithot);
        
        mCursor = managedQuery(mUri, PROJECTION, null, null, null);
        if (mCursor.moveToFirst()){
            //mid_prod.setTextKeepState(mCursor.getString(note));
            mcod_prod.setTextKeepState(mCursor.getString(COLUMN_INDEX_cod_prod));
            mnum_serie.setTextKeepState(mCursor.getString(COLUMN_INDEX_num_serie));
            mdescrip_prod.setTextKeepState(mCursor.getString(COLUMN_INDEX_descrip_prod));
            mid_subcat.setTextKeepState(mCursor.getString(COLUMN_INDEX_id_subcat));
            mnom_prod.setTextKeepState(mCursor.getString(COLUMN_INDEX_nom_prod));
            mmarca_prod.setTextKeepState(mCursor.getString(COLUMN_INDEX_marca_prod));
            mmod_prod.setTextKeepState(mCursor.getString(COLUMN_INDEX_mod_prod));
            mcont_uni.setTextKeepState(mCursor.getString(COLUMN_INDEX_cont_uni));
            muni_min.setTextKeepState(mCursor.getString(COLUMN_INDEX_uni_min));
            mcont_uni_med.setTextKeepState(mCursor.getString(COLUMN_INDEX_cont_uni_med));
            mcontenido.setTextKeepState(mCursor.getString(COLUMN_INDEX_contenido));
            mpeso_neto.setTextKeepState(mCursor.getString(COLUMN_INDEX_peso_neto));
            malto_cm.setTextKeepState(mCursor.getString(COLUMN_INDEX_alto_cm));
            mancho_cm.setTextKeepState(mCursor.getString(COLUMN_INDEX_ancho_cm));
            mfondo_cm.setTextKeepState(mCursor.getString(COLUMN_INDEX_fondo_cm));
            mfabr.setTextKeepState(mCursor.getString(COLUMN_INDEX_fabr));
            mfam_nec.setTextKeepState(mCursor.getString(COLUMN_INDEX_fam_nec));
            mfam_prod .setTextKeepState(mCursor.getString(COLUMN_INDEX_fam_prod));
            mclase_prod .setTextKeepState(mCursor.getString(COLUMN_INDEX_clase_prod));
            mlinea_prod.setTextKeepState(mCursor.getString(COLUMN_INDEX_linea_prod));
            mtipo_prod.setTextKeepState(mCursor.getString(COLUMN_INDEX_tipo_prod));
            mimagen.setTextKeepState(mCursor.getString(COLUMN_INDEX_imagen));
            mhot.setTextKeepState(mCursor.getString(COLUMN_INDEX_hot));
        }
        // log!
        Log.e(TAG, "HELLO producto editor lleno las cajas de la vista");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "onresume.............");



        //traigo el id desde el intento
        final Intent intent = getIntent();
        int def =0;
		int id = getIntent().getIntExtra("id", def );
        

        // Set the layout for this activity.  You can find it in res/layout/note_editor.xml
        Log.e(TAG, "setContentView prodeditor  " + id);
        setContentView(R.layout.prodeditor);
        
        // asigno textbox a variable corta
        mcod_prod = (EditText) findViewById(R.id.editcod_prod);
        mnum_serie = (EditText) findViewById(R.id.editnum_serie);
        mdescrip_prod = (EditText) findViewById(R.id.editdescrip_prod);
        mid_subcat = (EditText) findViewById(R.id.editid_subcat);
        mnom_prod = (EditText) findViewById(R.id.editnom_prod);
        mmarca_prod = (EditText) findViewById(R.id.editmarca_prod);
        mmod_prod = (EditText) findViewById(R.id.editmod_prod);
        mcont_uni = (EditText) findViewById(R.id.editcont_uni);
        muni_min = (EditText) findViewById(R.id.edituni_min);
        mcont_uni_med = (EditText) findViewById(R.id.editcont_uni_med);
        mcontenido = (EditText) findViewById(R.id.editcontenido);
        mpeso_neto = (EditText) findViewById(R.id.editpeso_neto);
        malto_cm = (EditText) findViewById(R.id.editalto_cm);
        mancho_cm = (EditText) findViewById(R.id.editancho_cm);
        mfondo_cm = (EditText) findViewById(R.id.editfondo_cm);
        mfabr = (EditText) findViewById(R.id.editfabr);
        mfam_nec = (EditText) findViewById(R.id.editfam_nec);
        mfam_prod = (EditText) findViewById(R.id.editfam_prod);
        mclase_prod = (EditText) findViewById(R.id.editclase_prod);
        mlinea_prod = (EditText) findViewById(R.id.editlinea_prod);
        mtipo_prod = (EditText) findViewById(R.id.edittipo_prod);
        mimagen = (EditText) findViewById(R.id.editimagen);
        mhot = (EditText) findViewById(R.id.edithot);

        mCursor = managedQuery(mUri, PROJECTION, null, null, null);
        if (mCursor.moveToFirst()){
            //mid_prod.setTextKeepState(mCursor.getString(note));
            mcod_prod.setTextKeepState(mCursor.getString(COLUMN_INDEX_cod_prod));
            mnum_serie.setTextKeepState(mCursor.getString(COLUMN_INDEX_num_serie));
            mdescrip_prod.setTextKeepState(mCursor.getString(COLUMN_INDEX_descrip_prod));
            mid_subcat.setTextKeepState(mCursor.getString(COLUMN_INDEX_id_subcat));
            mnom_prod.setTextKeepState(mCursor.getString(COLUMN_INDEX_nom_prod));
            mmarca_prod.setTextKeepState(mCursor.getString(COLUMN_INDEX_marca_prod));
            mmod_prod.setTextKeepState(mCursor.getString(COLUMN_INDEX_mod_prod));
            mcont_uni.setTextKeepState(mCursor.getString(COLUMN_INDEX_cont_uni));
            muni_min.setTextKeepState(mCursor.getString(COLUMN_INDEX_uni_min));
            mcont_uni_med.setTextKeepState(mCursor.getString(COLUMN_INDEX_cont_uni_med));
            mcontenido.setTextKeepState(mCursor.getString(COLUMN_INDEX_contenido));
            mpeso_neto.setTextKeepState(mCursor.getString(COLUMN_INDEX_peso_neto));
            malto_cm.setTextKeepState(mCursor.getString(COLUMN_INDEX_alto_cm));
            mancho_cm.setTextKeepState(mCursor.getString(COLUMN_INDEX_ancho_cm));
            mfondo_cm.setTextKeepState(mCursor.getString(COLUMN_INDEX_fondo_cm));
            mfabr.setTextKeepState(mCursor.getString(COLUMN_INDEX_fabr));
            mfam_nec.setTextKeepState(mCursor.getString(COLUMN_INDEX_fam_nec));
            mfam_prod .setTextKeepState(mCursor.getString(COLUMN_INDEX_fam_prod));
            mclase_prod .setTextKeepState(mCursor.getString(COLUMN_INDEX_clase_prod));
            mlinea_prod.setTextKeepState(mCursor.getString(COLUMN_INDEX_linea_prod));
            mtipo_prod.setTextKeepState(mCursor.getString(COLUMN_INDEX_tipo_prod));
            mimagen.setTextKeepState(mCursor.getString(COLUMN_INDEX_imagen));
            mhot.setTextKeepState(mCursor.getString(COLUMN_INDEX_hot));
        }
        // log!
        Log.e(TAG, "HELLO producto editor lleno las cajas de la vista");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        Log.e(TAG, "save.............");
        // Save away the original text, so we still have it if the activity
        // needs to be killed while paused.
        //outState.putString(ORIGINAL_CONTENT, mOriginalContent);
    }

    @Override
    protected void onPause() {
        Log.e(TAG, "pause.............");
        super.onPause();

        // The user is going somewhere else, so make sure their current
        // changes are safely saved away in the provider.  We don't need
        // to do this if only editing.
        /*if (mCursor != null) {
            String text = mText.getText().toString();
            String id_prod = mid_prod.getText().toString();
            String cod_prod = mcod_prod.getText().toString();
            String num_serie = mnum_serie.getText().toString();
            String descrip_prod = mdescrip_prod.getText().toString();
            String id_subcat = mid_subcat.getText().toString();
            String nom_prod = mnom_prod.getText().toString();
            String marca_prod = mmarca_prod.getText().toString();
            String mod_prod = mmod_prod.getText().toString();
            String cont_uni = mcont_uni.getText().toString();
            String uni_min = muni_min.getText().toString();
            String cont_uni_med = mcont_uni_med.getText().toString();
            String contenido = mcontenido.getText().toString();
            String peso_neto = mpeso_neto.getText().toString();
            String alto_cm = malto_cm.getText().toString();
            String ancho_cm = mancho_cm.getText().toString();
            String fondo_cm = mfondo_cm.getText().toString();
            String fabr = mfabr.getText().toString();
            String fam_nec = mfam_nec.getText().toString();
            String fam_prod = mfam_prod.getText().toString();
            String clase_prod = mclase_prod.getText().toString();
            String linea_prod = mlinea_prod.getText().toString();
            String tipo_prod = mtipo_prod.getText().toString();
            String imagen = mimagen.getText().toString();
            String hot = mhot.getText().toString();
            int length = text.length();

            // If this activity is finished, and there is no text, then we
            // do something a little special: simply delete the note entry.
            // Note that we do this both for editing and inserting...  it
            // would be reasonable to only do it when inserting.
            if (isFinishing() && (length == 0) && !mNoteOnly) {
                setResult(RESULT_CANCELED);
                deleteNote();

            // Get out updates into the provider.
            } else {
                ContentValues values = new ContentValues();

                // This stuff is only done when working with a full-fledged note.
                if (!mNoteOnly) {
                    // Bump the modification time to now.
                    values.put(productos.nom_prod, System.currentTimeMillis());
                }

                // Write our text back into the provider.
                values.put(productos.id_prod, id_prod);
                values.put(productos.cod_prod, cod_prod);
                values.put(productos.num_serie, num_serie);
                values.put(productos.descrip_prod, descrip_prod);
                values.put(productos.id_subcat, id_subcat);
                values.put(productos.nom_prod, nom_prod);
                values.put(productos.marca_prod, marca_prod);
                values.put(productos.mod_prod, mod_prod);
                values.put(productos.cont_uni, cont_uni);
                values.put(productos.uni_min, uni_min);
                values.put(productos.cont_uni_med, cont_uni_med);
                values.put(productos.contenido, contenido);
                values.put(productos.peso_neto, peso_neto);
                values.put(productos.alto_cm, alto_cm);
                values.put(productos.ancho_cm, ancho_cm);
                values.put(productos.fondo_cm, fondo_cm);
                values.put(productos.fabr, fabr);
                values.put(productos.fam_nec, fam_nec);
                values.put(productos.fam_prod, fam_prod);
                values.put(productos.clase_prod, clase_prod);
                values.put(productos.linea_prod, linea_prod);
                values.put(productos.tipo_prod, tipo_prod);
                values.put(productos.imagen, imagen);
                values.put(productos.hot, hot);

                // Commit all of our changes to persistent storage. When the update completes
                // the content provider will notify the cursor of the change, which will
                // cause the UI to be updated.
                getContentResolver().update(mUri, values, null, null);
            }
        }*/
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        Log.e(TAG, "menuoptions.............");
        super.onCreateOptionsMenu(menu);

        // Build the menus that are shown when editing.
        if (mState == STATE_EDIT) {
            menu.add(0, ADD_ID, 0, R.string.menu_insert)
                    .setShortcut('0', 'r')
                    .setIcon(android.R.drawable.ic_menu_add);
            if (!mNoteOnly) {
                menu.add(0, DELETE_ID, 0, R.string.menu_delete)
                        .setShortcut('1', 'd')
                        .setIcon(android.R.drawable.ic_menu_delete);
            }

        // Build the menus that are shown when inserting.
        } else {
            menu.add(0, DISCARD_ID, 0, R.string.menu_discard)
                    .setShortcut('0', 'd')
                    .setIcon(android.R.drawable.ic_menu_delete);
        }

        // If we are working on a full note, then append to the
        // menu items for any other activities that can do stuff with it
        // as well.  This does a query on the system for any activities that
        // implement the ALTERNATIVE_ACTION for our data, adding a menu item
        // for each one that is found.
        if (!mNoteOnly) {
            Intent intent = new Intent(null, getIntent().getData());
            intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
            menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0,
                    new ComponentName(this, Productoeditor.class), null, intent, 0, null);
        }

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.e(TAG, "itemselected.............");
        // Handle all of the possible menu actions.
        switch (item.getItemId()) {
        case DELETE_ID:
        	addProd();
            finish();
            break;
        case DISCARD_ID:
            cancelNote();
            break;
        case ADD_ID:
        	addProd();
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * Take care of canceling work on a note.  Deletes the note if we
     * had created it, otherwise reverts to the original text.
     */
    private final void cancelNote() {
        Log.e(TAG, "cancelnote.............");
        if (mCursor != null) {
            if (mState == STATE_EDIT) {
                // Put the original note text back into the database
                mCursor.close();
                mCursor = null;
                ContentValues values = new ContentValues();
                values.put(productos.hot, mOriginalContent);
                getContentResolver().update(mUri, values, null, null);
            } else if (mState == STATE_INSERT) {
                // We inserted an empty note, make sure to delete it
            	addProd();
            }
        }
        setResult(RESULT_CANCELED);
        finish();
    }

    /**
     * Take care of deleting a note.  Simply deletes the entry.
     */
    private final void addProd() {
        Log.e(TAG, "add producto.............");
        ContentValues values;
        values = new ContentValues();
        //values.put(productos.id_prod, mid_prod.getText().toString());
        values.put(productos.cod_prod, mcod_prod.getText().toString());
        values.put(productos.num_serie, mnum_serie.getText().toString());
        values.put(productos.descrip_prod, mdescrip_prod.getText().toString());
        values.put(productos.id_subcat, mid_subcat.getText().toString());
        values.put(productos.nom_prod, mnom_prod.getText().toString());
        values.put(productos.marca_prod, mmarca_prod.getText().toString());
        values.put(productos.mod_prod, mmod_prod.getText().toString());
        values.put(productos.cont_uni, mcont_uni.getText().toString());
        values.put(productos.uni_min, muni_min.getText().toString());
        values.put(productos.cont_uni_med, mcont_uni_med.getText().toString());
        values.put(productos.contenido, mcontenido.getText().toString());
        values.put(productos.peso_neto, mpeso_neto.getText().toString());
        values.put(productos.alto_cm, malto_cm.getText().toString());
        values.put(productos.ancho_cm, mancho_cm.getText().toString());
        values.put(productos.fondo_cm, mfondo_cm.getText().toString());
        values.put(productos.fabr, mfabr.getText().toString());
        values.put(productos.fam_nec, mfam_nec.getText().toString());
        values.put(productos.fam_prod, mfam_prod.getText().toString());
        values.put(productos.clase_prod, mclase_prod.getText().toString());
        values.put(productos.linea_prod, mlinea_prod.getText().toString());
        values.put(productos.tipo_prod, mtipo_prod.getText().toString());
        values.put(productos.imagen, mimagen.getText().toString());
        values.put(productos.hot, mhot.getText().toString());
            getContentResolver().insert(mUri, values);
            //mText.setText("");
    }
}

